package cn.javacv.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.javacv.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.javacv.gulimall.product.entity.AttrEntity;
import cn.javacv.gulimall.product.service.AttrService;
import cn.javacv.gulimall.product.service.CategoryService;
import cn.javacv.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.javacv.gulimall.product.entity.AttrGroupEntity;
import cn.javacv.gulimall.product.service.AttrGroupService;
import cn.javacv.gulimall.common.utils.PageUtils;
import cn.javacv.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author dingqiang
 * @email 295213096@qq.com
 * @date 2020-08-14 07:22:12
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") long categoryId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrGroupEntity attrGroup = attrGroupService.getById(id);
        Long [] paths = categoryService.findCategoryPath(attrGroup.getCategoryId());
        attrGroup.setCategoryIdPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelationList(@PathVariable("attrgroupId") long attrgroupId){
        List<AttrEntity> list =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",list);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelationPage(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") long attrgroupId){
        PageUtils page  =  attrService.getNoRelationPage(params, attrgroupId);
        return R.ok().put("page",page);
    }

    //product/attrgroup/225/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithattr(@PathVariable("catelogId") long catelogId){
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOList  =  attrGroupService.getAttrGroupWithAttrByCategoryId(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVOList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrGroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
